package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.CouponModel;
import com.btkjgc.models.extend.CouponReceiveModel;
import com.btkjgc.models.extend.ReturnMoneyModel;
import com.btkjgc.service.intf.CouponManageService_Intf;

@Controller
public class CouponManageController {

	static Logger logger = Logger.getLogger(CouponManageController.class);

    @Autowired
	private CouponManageService_Intf couponManageService;

	@RequestMapping(value = "coupon_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,CouponModel couponModel) {

		ModelAndView mv = new ModelAndView("coupon_manage");
		getCouponList(request, mv,couponModel);
		return mv;
	}
	
 
	@RequestMapping(value = "coupon_receive_list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getCouponReceiveRecords(HttpServletRequest request,CouponReceiveModel couponReceiveModel) {

		ModelAndView mv = new ModelAndView("coupon_receive_list");
		getCouponReceiveList(request, mv,couponReceiveModel);
		return mv;
	}
	
	
	private void getCouponReceiveList(HttpServletRequest request,
			ModelAndView mv, CouponReceiveModel couponReceiveModel) {
		// TODO Auto-generated method stub
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "tel","like", couponReceiveModel.getTel()};
		Object[] arr2 = { "account","like", couponReceiveModel.getAccount()};
		paramList.add(arr1);
		paramList.add(arr2);
		
        //个人优惠券和公司优惠券union  all
		String sql =" select * from(\n"+
				    " SELECT\n" +
					"	crt.*, aut. NAME accountName,aut. account,\n" +
					"  case crt.used\n" +
					"   when 1 then '已使用'\n" +
					"   when 0 then '未使用'\n" +
					" end usedName, aut.tel,cat.carbrand carplate,\n" +
					" ct.name cartype, aut2.name companyName \n" +
					"FROM\n" +
					"	coupon_receive_t crt\n" +
					"LEFT JOIN account_user_t aut ON aut.id = crt.accountid AND aut.del = 0 \n" +
					"LEFT JOIN coupon_t coupt on coupt.id=crt.accountid and coupt.del=0\n" +
					"LEFT JOIN car_account_t cat on cat.accountid=crt.accountid and cat.del=0\n" +
					"LEFT JOIN cartype_t ct on ct.del=0 and cat.cartypeid = ct.id and ct.del=0\n" +
					"LEFT JOIN car_t cart on cart.del=0 and cart.id=cat.carid   AND cart.del = 0 \n" +
					"LEFT JOIN account_user_t aut2 ON cart.companyid = aut2.id  AND aut2.del = 0\n" +
					"WHERE\n" +
					"	crt.del = 0 and aut.type=0\n" +
					
					"union all  \n" +
					
					"(SELECT\n" +
					"	crt.*, aut.name accountName,\n" +
					"	aut.account,\n" +
					"	CASE crt.used\n" +
					"WHEN 1 THEN\n" +
					"	'已使用'\n" +
					"WHEN 0 THEN\n" +
					"	'未使用'\n" +
					"END usedName,\n" +
					" aut.tel,\n" +
					" cart.carplate, ct.name cartype,aut. NAME companyName\n" +
					"FROM\n" +
					"	coupon_receive_t crt\n" +
					"LEFT JOIN account_user_t aut ON aut.id = crt.accountid AND aut.del = 0\n" +
					"LEFT JOIN car_t cart ON cart.del = 0\n" +
					"AND cart.companyid = aut.id\n" +
					"AND cart.del = 0\n" +
					"LEFT JOIN cartype_t ct ON ct.del = 0 AND  cart.cartypeid = ct.id AND ct.del = 0\n" +
					"WHERE\n" +
					"	crt.del = 0\n" +
					"AND aut.type = 1  limit 1)\n" +
				     " )a\n"+
					"where couponid="+couponReceiveModel.getCouponid();
		          

		sql = couponManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = couponManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<CouponReceiveModel> couponReceiveModelList = couponManageService.getList(
				CouponReceiveModel.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		couponManageService.setCouponReceiveListData(request, mv, pageIndex, totalRecord, couponReceiveModelList);
	}


	@RequestMapping(value = "view_coupon", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView viewPckageFare(HttpServletRequest request,ReturnMoneyModel returnMoneyModel)
	{
		 ModelAndView mv = new ModelAndView("add_coupon");
		 
		 if ("edit".equals(returnMoneyModel.getTask())) {
			 couponManageService.setEditCoupondata(request,mv,returnMoneyModel.getId());
		 }
		
		 return mv;
	}

	@RequestMapping(value = "edit_coupon", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public AjaxObj editReturnMoney(CouponModel couponModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		AjaxObj ajaxObj=couponManageService.updateCouponModel(couponModel);

		return ajaxObj;
	}

 
	@SuppressWarnings("unchecked")
	private void getCouponList(HttpServletRequest request, ModelAndView mv,CouponModel couponModel) {
 
		String begindate = request.getParameter("begintime");
		String enddate = request.getParameter("endtime");
		String status = request.getParameter("status");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "ct.name","like", couponModel.getName()};
		Object[] arr2 = { "ct.begintime", "d<", begindate };
		Object[] arr3 = { "ct.endtime", "d>", enddate };
		Object[] arr4 = { "ct.status","=", status };
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);

		String sql ="SELECT\n" +
				"	ct.*, aut.username opername,\n" +
				"	CASE ct. STATUS\n" +
				"		WHEN 1 THEN '开启'\n" +
				"		WHEN 0 THEN '暂停'\n" +
				"	END statusName,\n" +
				"  CASE\n" +
				"	  WHEN ct.useenddate IS NULL THEN '不限'\n" +
				"	  ELSE ct.useenddate\n" +
				"  END showUseendDate,\n" +
				"  CASE\n" +
				"     WHEN ct.usebegindate IS NULL THEN '即时生效'\n" +
				"     ELSE ct.usebegindate\n" +
				"  END showUsebeginDate \n" +
				"FROM\n" +
				"	coupon_t ct\n" +
				"LEFT JOIN admin_user_t aut ON aut.id = ct.operid AND aut.del = 0\n" +
				"WHERE	ct.del = 0";

		sql = couponManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = couponManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<CouponModel> couponModelList = couponManageService.getList(
				CouponModel.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		couponManageService.setCouponListData(request, mv, pageIndex, totalRecord,
				couponModelList);

	}

	
 

	@RequestMapping(value = "add_coupon/{a}")
	@ResponseBody
	public   AjaxObj jsonAddCouponModel(HttpServletRequest request,
			@PathVariable("a") int a, CouponModel couponModel) {
 
		AjaxObj ajaxObj = null;
		if (couponModel.getName().length() > 0) {
 
			  ajaxObj = couponManageService
					.insertCouponModel(couponModel);
			

		}
		return ajaxObj;
	}

	
	@RequestMapping(value = "del_coupon", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String  delReturnMoney(HttpServletRequest request, String id) {
 
		couponManageService.updateDelById(id);
 
		return "redirect:/coupon_manage";
	}
	
	@RequestMapping(value = "update_status_coupon", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String pauseReturnMoney(HttpServletRequest request, String id,String status) {
 
		couponManageService.updateStatusById(id,status);
	 
		return "redirect:/coupon_manage";
	}
 
	
	
}
